package com.htao.train.business.service;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSON;
import com.htao.train.business.domain.ConfirmOrder;
import com.htao.train.business.dto.ConfirmOrderMQDto;
import com.htao.train.business.enums.ConfirmOrderStatusEnum;
import com.htao.train.business.enums.RocketMQTopicEnum;
import com.htao.train.business.mapper.ConfirmOrderMapper;
import com.htao.train.business.req.ConfirmOrderDoReq;
import com.htao.train.business.req.ConfirmOrderTicketReq;
import com.htao.train.common.context.LoginMemberContext;
import com.htao.train.common.exception.BusinessException;
import com.htao.train.common.exception.BusinessExceptionEnum;
import com.htao.train.common.util.SnowUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author： HTTT
 * @Date： 2024/1/27 16:27
 * @Describe：
 */
@Service
@Slf4j
public class BeforeConfirmOrderService {
    @Resource
    private ConfirmOrderMapper confirmOrderMapper;
    @Resource
    private SkTokenService skTokenService;
//    @Resource
//    private RocketMQTemplate rocketMQTemplate;
    @Resource
    private ConfirmOrderService confirmOrderService;

    public long beforeDoConfirm(ConfirmOrderDoReq req){
        //req中加入用户id
        req.setMemberId(LoginMemberContext.getId());
        //令牌大闸 -> 校验库存 并防止机器人刷票

        boolean validSkToken = skTokenService.validSkToken(req.getDate(),req.getTrainCode(),req.getMemberId());
        if(validSkToken){
            log.info("令牌校验通过");
        }else {
            log.info("令牌校验未通过");
            throw new BusinessException(BusinessExceptionEnum.CONFIRM_TOKEN_EXCEPTION);
        }
        DateTime now = DateTime.now();
        Date date = req.getDate();
        String code = req.getTrainCode();
        String start = req.getStart();
        String end = req.getEnd();
        List<ConfirmOrderTicketReq> tickets = req.getTickets();

        // 保存确认订单表，状态初始
        ConfirmOrder confirmOrder = new ConfirmOrder();
        confirmOrder.setId(SnowUtil.getSnowflakeNextId());
        confirmOrder.setMemberId(LoginMemberContext.getId());
        confirmOrder.setDate(date);
        confirmOrder.setTrainCode(code);
        confirmOrder.setStart(start);
        confirmOrder.setEnd(end);
        confirmOrder.setDailyTrainTicketId(req.getDailyTrainTicketId());
        confirmOrder.setStatus(ConfirmOrderStatusEnum.INIT.getCode());
        confirmOrder.setCreateTime(now);
        confirmOrder.setUpdateTime(now);
        confirmOrder.setTickets(JSON.toJSONString(tickets));
        confirmOrderMapper.insert(confirmOrder);


        //mq异步购票
        req.setLogId(MDC.get("LOG_ID"));
        //将req变为dto 无需传全部信息
        ConfirmOrderMQDto confirmOrderMQDto = new ConfirmOrderMQDto();
        confirmOrderMQDto.setLogId(MDC.get("LOG_ID"));
        confirmOrderMQDto.setDate(req.getDate());
        confirmOrderMQDto.setTrainCode(req.getTrainCode());
        log.info("排队购票，发送消息{}",confirmOrderMQDto);
        String dtoJSON = JSON.toJSONString(confirmOrderMQDto);
/*        rocketMQTemplate.convertAndSend(RocketMQTopicEnum.CONFIRM_ORDER.getCode(),dtoJSON);
        log.info("mq发送结束,日志跟踪号{}",MDC.get("LOG_ID"));*/
        confirmOrderService.doConfirm(confirmOrderMQDto);
        return confirmOrder.getId();
    }
}
